package com.spring.shops.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.spring.shops.context.BaseContext;
import com.spring.shops.dao.TzSysRoleMapper;
import com.spring.shops.dao.TzSysRoleMenuMapper;
import com.spring.shops.model.dto.SysRoleDTO;
import com.spring.shops.model.dto.SysSaveRoleDTO;
import com.spring.shops.model.entity.TzSysRole;
import com.spring.shops.model.entity.TzSysRoleMenu;
import com.spring.shops.model.entity.TzSysUser;
import com.spring.shops.model.result.IPageIndexImg;
import com.spring.shops.model.result.ServerResponseEntityVoid;
import com.spring.shops.model.vo.SysRoleVO;
import com.spring.shops.model.vo.SysUserVO;
import com.spring.shops.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private TzSysRoleMapper mapper;
    @Autowired
    private TzSysRoleMenuMapper roleMenuMapper;
    /**
     * 列出所有的角色
     * @return
     */
    @Override
    public ServerResponseEntityVoid<List<SysRoleVO>> list() {
        List<TzSysRole> roleList=mapper.selectAll();
        List<SysRoleVO> roleListVO=new ArrayList<>();
        //进行属性拷贝
        for (TzSysRole tzSysRole : roleList) {
            roleListVO.add(getRoleVO(tzSysRole));
        }
        return ServerResponseEntityVoid.success(roleListVO);
    }
    /**
     * 角色列表分页查询
     * @param dto
     * @return
     */
    @Override
    public ServerResponseEntityVoid<IPageIndexImg<SysRoleVO>> page(SysRoleDTO dto) {
        //获取当前页码
        Long currentPage = dto.getCurrent();
        //获取每页显示的条数
        Long size= dto.getSize();
        //设置分页初始值
        PageHelper.startPage(Math.toIntExact(currentPage), Math.toIntExact(size));
        //调用mapper层进行分页查询
        Page<TzSysRole> rolePage=mapper.page(dto);
        //创建IPageIndexImg对象
        IPageIndexImg<SysRoleVO> iPageIndexImg=new IPageIndexImg<>();
        iPageIndexImg.setSize(size);
        iPageIndexImg.setTotal( rolePage.getTotal());
        iPageIndexImg.setCurrent(currentPage);
        iPageIndexImg.setPages( rolePage.getTotal()/size+1);
        //获取所有记录
        List<TzSysRole> result =  rolePage.getResult();
        List<SysRoleVO> data = new ArrayList<>();
        //属性拷贝
        for (TzSysRole role : result) {
            SysRoleVO roleVO = getRoleVO(role);
            //加入集合中
            data.add(roleVO);
        }
        iPageIndexImg.setRecords(data);
        //返回结果集
        return ServerResponseEntityVoid.success(iPageIndexImg);
    }
    /**
     * 新增角色
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public ServerResponseEntityVoid save(SysSaveRoleDTO dto) {
        //先进行角色的新增
        TzSysRole role = new TzSysRole();
        role.setRoleName(dto.getRoleName());
        role.setRemark(dto.getRemark());
        role.setCreateUserId(BaseContext.getCurrentId());
        role.setCreateTime(new Date());
        int rows = mapper.insert(role);
        //获取新增角色id
        Long roleId = role.getRoleId();
        //再进行角色权限列表的新增
        List<Integer> menuIdList = dto.getMenuIdList();
        for (Integer menuId : menuIdList) {
            Long menuId2= Long.valueOf(menuId);
            TzSysRoleMenu tzSysRoleMenu = new TzSysRoleMenu(roleId, menuId2);
            roleMenuMapper.insert(tzSysRoleMenu);
        }
        return ServerResponseEntityVoid.success();
    }
    /**
     * 根据id删除角色
     * @param integers
     * @return
     */
    @Override
    @Transactional
    public ServerResponseEntityVoid deleteByIds(List<Long> integers) {
        for (Long roleId : integers) {
            //先根据id删除角色
            int rows = mapper.deleteByPrimaryKey(roleId);
            //再将对应角色权限表中roleId等于当前id删除掉
            int rows2=roleMenuMapper.deleteByRoleId(roleId);
        }
        return ServerResponseEntityVoid.success();
    }
    /**
     * 根据id获取信息
     * @param roleId
     * @return
     */
    @Override
    public ServerResponseEntityVoid<SysRoleVO> getInfoById(Long roleId) {
        //先根据id获取角色信息
        TzSysRole tzSysRole = mapper.selectByPrimaryKey(roleId);
        SysRoleVO roleVO = getRoleVO(tzSysRole);
        //再根据roleId获取角色权限表中的数据
        List<Integer> menuIdList=roleMenuMapper.selectByRoleId(roleId);
        roleVO.setMenuIdList(menuIdList);
        return ServerResponseEntityVoid.success(roleVO);
    }
    /**
     * 根据id更新信息
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public ServerResponseEntityVoid updateInfoById(SysSaveRoleDTO dto) {
        //先更新角色信息
        TzSysRole tzSysRole = new TzSysRole(dto.getRoleId(), dto.getRoleName(), dto.getRemark(), BaseContext.getCurrentId(),
                new Date());
        mapper.updateByPrimaryKey(tzSysRole);
        //再根据roleId进行角色权限列表的更新(先删除再插入）
        roleMenuMapper.deleteByRoleId(dto.getRoleId());
        List<Integer> menuIdList = dto.getMenuIdList();
        for (Integer menuId : menuIdList) {
            Long menuId2= Long.valueOf(menuId);
            //插入数据
            roleMenuMapper.insert(new TzSysRoleMenu(dto.getRoleId(),menuId2));
        }
        return ServerResponseEntityVoid.success();
    }

    //entity->VO
    public SysRoleVO getRoleVO(TzSysRole role){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return new SysRoleVO(
                role.getRoleId(),role.getRoleName(),role.getRemark(),
                null,sdf.format(role.getCreateTime())
        );
    }
}
